/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Payment.java
 *
 * Created on Oct 30, 2013, 8:38:54 PM
 */
package sims.gui;

import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import sims.dao.PaymentDao;
import sims.entity.TbPayment;
import sims.library.Validate;

/**
 *
 * @author NINH
 */
public class Payment extends javax.swing.JPanel {

    PaymentDao payDao;
    Validate valid;

    public Payment() {
        initComponents();
        payDao = new PaymentDao();
        valid = new Validate();
        loadData();
        disableTextbox();
    }

    public void loadData() {
        tblPayment.setModel(DisplayAll());
    }

    public DefaultTableModel DisplayAll() {
        Vector vtable = new Vector();
        Vector vrow = new Vector();
        Vector vheader = new Vector();
        List<TbPayment> payments = payDao.getAll(TbPayment.class);

        vheader.add("Id");
        vheader.add("Name");

        for (TbPayment pay : payments) {
            vrow.add(pay.getPayId());
            vrow.add(pay.getPayName());
            vtable.add(vrow);
            vrow = new Vector();
        }
        return new DefaultTableModel(vtable, vheader);
    }

    public void Reset() {
        txtPayId.setText("");
        txtPayName.setText("");
    }

    public void binding() {
        int row = tblPayment.getSelectedRow();
        String id = tblPayment.getValueAt(row, 0).toString();
        String name = tblPayment.getValueAt(row, 1).toString();

        txtPayId.setText(id);
        txtPayName.setText(name);
    }

    public void disableTextbox() {
        txtPayName.setEnabled(false);
    }

    public void enableTextbox() {
        txtPayName.setEnabled(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        lblPayName = new javax.swing.JLabel();
        txtPayName = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        btnAdd = new javax.swing.JButton();
        btnUpdate = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        lblPayId = new javax.swing.JLabel();
        txtPayId = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblPayment = new javax.swing.JTable();

        setBackground(new java.awt.Color(255, 255, 255));
        setBorder(javax.swing.BorderFactory.createTitledBorder("Payment manager"));
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        lblPayName.setFont(new java.awt.Font("Tahoma", 2, 11));
        lblPayName.setForeground(new java.awt.Color(255, 0, 0));
        jPanel1.add(lblPayName, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 40, 460, 20));
        jPanel1.add(txtPayName, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 40, 310, -1));

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel7.setText("Payment name: ");
        jPanel1.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40, 100, 20));

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sims/images/add.png"))); // NOI18N
        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });
        jPanel1.add(btnAdd, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 70, 90, 24));

        btnUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sims/images/edit.png"))); // NOI18N
        btnUpdate.setText("Edit");
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });
        jPanel1.add(btnUpdate, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 70, 100, 24));

        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sims/images/delete.png"))); // NOI18N
        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });
        jPanel1.add(btnDelete, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 70, -1, 24));

        lblPayId.setFont(new java.awt.Font("Tahoma", 2, 11));
        lblPayId.setForeground(new java.awt.Color(255, 0, 0));
        jPanel1.add(lblPayId, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 10, 460, 20));

        txtPayId.setEnabled(false);
        jPanel1.add(txtPayId, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 10, 310, -1));

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel8.setText("Payment Id: ");
        jPanel1.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 100, 20));

        add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 280, 1030, 260));

        tblPayment.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblPayment);

        add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, 1030, 250));
    }// </editor-fold>//GEN-END:initComponents

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        // TODO add your handling code here:
        if (btnDelete.getText().equalsIgnoreCase("delete")) {
            if (tblPayment.getSelectedRow() == -1) {
                JOptionPane.showMessageDialog(null, "Select payment to delete.");
                return;
            } else {
                int check = JOptionPane.showConfirmDialog(null, "Are you sure");
                if (check == JOptionPane.OK_OPTION) {
                    int rowIndex = tblPayment.getSelectedRow();
                    int payId = Integer.parseInt(tblPayment.getValueAt(rowIndex, 0).toString());
                    TbPayment pay = (TbPayment) payDao.getById(TbPayment.class, payId);
                    payDao.delete(pay);
                    JOptionPane.showMessageDialog(null, "Delete success.");
                    loadData();
                }
            }
        } else {
            disableTextbox();
            tblPayment.setEnabled(true);
            btnAdd.setText("Add");
            btnAdd.setEnabled(true);
            btnUpdate.setText("Edit");
            btnUpdate.setEnabled(true);
            btnDelete.setText("Delete");
            Reset();
        }
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
        if (btnAdd.getText().equalsIgnoreCase("add")) {
            enableTextbox();
            Reset();
            btnAdd.setText("Save");
            tblPayment.setEnabled(false);
            btnUpdate.setEnabled(false);
            btnDelete.setText("Cancel");
        } else {
            try {
                String payName = txtPayName.getText();

                TbPayment pay = new TbPayment();
                pay.setPayName(payName);

                payDao.insert(pay);
                JOptionPane.showMessageDialog(null, "Insert success.");
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "Insert error.");
            }
            loadData();
            disableTextbox();
            Reset();
            btnAdd.setText("Add");
            tblPayment.setEnabled(true);
            btnUpdate.setEnabled(true);
            btnDelete.setText("Delete");
        }
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
        // TODO add your handling code here:

        if (btnUpdate.getText().equalsIgnoreCase("edit")) {
            if (tblPayment.getSelectedRow() == -1) {
                JOptionPane.showMessageDialog(null, "Select payment to edit.");
                return;
            } else {
                enableTextbox();
                binding();
                btnUpdate.setText("Save");
                tblPayment.setEnabled(false);
                btnAdd.setEnabled(false);
                btnDelete.setText("Cancel");
            }
        } else {
            int id = Integer.parseInt(txtPayId.getText());
            String name = txtPayName.getText();

            TbPayment pay = new TbPayment();
            pay.setPayId(id);
            pay.setPayName(name);

            payDao.update(pay);
            JOptionPane.showMessageDialog(null, "Update success.");
            loadData();
            disableTextbox();
            Reset();

            btnUpdate.setText("Edit");
            tblPayment.setEnabled(true);
            btnAdd.setEnabled(true);
            btnDelete.setText("Delete");
        }
    }//GEN-LAST:event_btnUpdateActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblPayId;
    private javax.swing.JLabel lblPayName;
    private javax.swing.JTable tblPayment;
    private javax.swing.JTextField txtPayId;
    private javax.swing.JTextField txtPayName;
    // End of variables declaration//GEN-END:variables
}
